package com.fuxi.cloud.common.util;

/**
 * ApiAssert
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-04-19
 */
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Objects;

import com.fuxi.cloud.common.expetion.JimuAlertException;
import com.fuxi.cloud.common.vo.Result;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;

import org.springframework.util.CollectionUtils;

public class ApiAssert {
    public static void eq(String info, Object obj1, Object obj2) {
        if (!Objects.equals(obj1, obj2)) {
            failure(info);
        }

    }

    public static void isTrue(String info, boolean condition) {
        if (!condition) {
            failure(info);
        }

    }

    public static void isFalse(String info, boolean condition) {
        if (condition) {
            failure(info);
        }

    }

    public static void gtzero(String info, int n) {
        if (n <= 0) {
            failure(info);
        }

    }

    public static void lezero(String info, int n) {
        if (n > 0) {
            failure(info);
        }

    }

    public static void gtzero(String info, BigDecimal n) {
        if (CommonUtils.le(n, new BigDecimal(0))) {
            failure(info);
        }

    }

    public static void gezero(String info, BigDecimal n) {
        if (CommonUtils.le(n, new BigDecimal(0))) {
            failure(info);
        }

    }

    public static void isEmpty(String info, Object obj) {
        if (ObjectUtils.isNotEmpty(obj)) {
            failure(info);
        }

    }

    public static void isNotEmpty(String info, Object obj) {
        if (ObjectUtils.isEmpty(obj)) {
            failure(info);
        }

    }

    public static void anyOneIsNull(String info, Object... conditions) {
        if (CommonUtils.allNotNull(conditions)) {
            failure(info);
        }

    }

    public static void anyOneIsNotNull(String info, Object... conditions) {
        if (CommonUtils.allIsNull(conditions)) {
            failure(info);
        }

    }

    public static void allNotNull(String info, Object... conditions) {
        if (CommonUtils.anyOneIsNull(conditions)) {
            failure(info);
        }

    }

    private static void failure(Result j) {
        throw new JimuAlertException(1001, j.getMessage());
    }

    public static void failure(String info) {
        throw new JimuAlertException(1001, info);
    }

    public static void notEmpty(Result j, Object[] array) {
        if (ArrayUtils.isEmpty(array)) {
            failure(j);
        }

    }

    public static void noNullElements(Result j, Object[] array) {
        if (array != null) {
            Object[] var2 = array;
            int var3 = array.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                Object var5 = var2[var4];
                if (var5 == null) {
                    failure(j);
                }
            }
        }

    }

    public static void notEmpty(Result j, Collection<?> collection) {
        if (CollectionUtils.isEmpty(collection)) {
            failure(j);
        }

    }

    private ApiAssert() {
    }
}
